//
//  RefundViewController.m
//  phmall
//
//  Created by admin on 2017/12/16.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "EvaluationDetailsVC.h"
#import "Orderheader.h"
#import "ApplyRefundVC.h"
#import "EvaluationDetailsCell.h"
#import "RefundScheduleVC.h"
#import "EvaluateModel.h"
#import "EvaluationLisModel.h"
@interface EvaluationDetailsVC ()<UITableViewDelegate,UITableViewDataSource>
{
    Orderheader *_headView;
    NSString    *type;
}
@property(nonatomic,strong)UITableView *myTableview;
@property(nonatomic,strong)MBProgressHUD *hud;
@property(nonatomic,strong)NSMutableArray *EvaluationArray;
@end

@implementation EvaluationDetailsVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navBarBgAlpha=@"1.0";
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    type=@"buyer";
    [self refreshEvaluation:YES];
    self.title=Localized(@"评价管理");
    UIBarButtonItem* leftBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem=leftBar;
    [self setUI];
    self.myTableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(HeaderRefresh)];
    // Do any additional setup after loading the view.
}
-(void)HeaderRefresh{
    [self refreshEvaluation:NO];
}
-(void)setUI{
    //消除强引用
    __weak typeof(self) weakSelf = self;
    _headView = [[Orderheader alloc]init];
    [self.view addSubview:_headView];
    [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.left.right.equalTo(self.view);
        make.height.offset(40);
    }];
   
    _headView.backgroundColor=[UIColor colorWithHexString:@"#f8fafd"];
    _headView.itemClickAtIndex = ^(NSInteger index){
        weakSelf.hud=[MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow  animated:YES];
        weakSelf.hud.detailsLabel.text =  Localized(@"加载中...");
        switch (index) {
            case 0:
                type=@"buyer";
                [weakSelf refreshEvaluation:YES];
                break;
            case 1:
                type=@"seller";
                [weakSelf refreshEvaluation:YES];
                break;
            default:
                break;
        }
    };
    _headView.items = @[Localized(@"来自买家的评价"),Localized(@"来自卖家的评价")];
    [_headView setSelectAtIndex:self.items];
    [self.view addSubview:self.myTableview];
    [self.myTableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(40);
        make.left.right.equalTo(self.view).priorityHigh();
        make.bottom.equalTo(self.view).with.offset(-self.tabBar_height).priorityHigh();
    }];
}

- (void)refreshEvaluation:(BOOL)reload{
    NSString *urlstr =  [NSString stringWithFormat:@"%@%@%@",HomeURL,@"evaluation_list/",type];
    [httpTool ZBPostNetDataWith:urlstr withDic:nil andSuccess:^(id responseObject) {
        NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        if ([[NSString stringWithFormat:@"%@",responseDict[@"ret"]] isEqualToString:@"200"]) {
            NSMutableArray *data=responseDict[@"data"];
            if (data.count==0) {
                [self.myTableview.mj_header endRefreshing];
                if (reload) {
                    [self.myTableview reloadData];
                }
            }else{
                [self analysisDataWithEvaluationArray:data];
            }
            
        }
        [self.hud hideAnimated:YES];
    } andFailure:^{
        [self.hud hideAnimated:YES];
    }];
}
- (void)analysisDataWithEvaluationArray:(NSMutableArray *)array{
    [self.EvaluationArray removeAllObjects];
    for (NSMutableDictionary *dict in array) {
        EvaluationLisModel *model=[EvaluationLisModel yy_modelWithDictionary:dict];
        [self.EvaluationArray addObject:model];
    }
    [self.myTableview.mj_header endRefreshing];
    [self.myTableview reloadData];
}
#pragma mark - tableView
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.EvaluationArray.count;;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //    OrderModel *model=self.orderList[section];
    //    NSMutableArray *data=model.goods_list;
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EvaluationDetailsCell *cell=[EvaluationDetailsCell theShareCellWithTableView:tableView];

    EvaluationLisModel *model=self.EvaluationArray[indexPath.section];
//        NSMutableArray *data=model.goods_list;
//        GoodsListModel *goodsModel=data[indexPath.row];
    cell.model=model;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.hidesBottomBarWhenPushed=YES;
    ApplyRefundVC *evaluate=[[ApplyRefundVC alloc]init];
    [self.navigationController pushViewController:evaluate animated:YES];
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    //分割线补全
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
//MARK:-GETTER
- (UITableView *)myTableview{
    if (!_myTableview) {
        _myTableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableview.delegate = self;
        _myTableview.dataSource = self;
        _myTableview.backgroundColor=[UIColor groupTableViewBackgroundColor];
        _myTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableview.showsVerticalScrollIndicator=NO;
        _myTableview.scrollEnabled = YES;
    }
    return _myTableview;
}
-(NSMutableArray *)EvaluationArray{
    if (!_EvaluationArray) {
        _EvaluationArray=[[NSMutableArray alloc]init];
    }
    return _EvaluationArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

